﻿using ConfOrm;
using ConfOrm.NH;
using ConfOrm.Patterns;
using ConfOrm.Shop.Appliers;
using ConfOrm.Shop.CoolNaming;
using ConfOrm.Shop.InflectorNaming;
using ConfOrm.Shop.Inflectors;
using ConfOrm.Shop.Packs;

namespace NHDay_Linq.ConfORM
{
	public class DomainMapper : IDomainMapper
	{
		private readonly ObjectRelationalMapper _orm;
		private readonly Mapper _mapper;
		public DomainMapper()
		{
			_orm = new ObjectRelationalMapper();

			IPatternsAppliersHolder patternsAppliers =
								(new SafePropertyAccessorPack())
										.Merge(new OneToOneRelationPack(_orm))
										.Merge(new BidirectionalManyToManyRelationPack(_orm))
										.Merge(new BidirectionalOneToManyRelationPack(_orm))
										.Merge(new DiscriminatorValueAsClassNamePack(_orm))
										.Merge(new CoolColumnsNamingPack(_orm))
										.Merge(new TablePerClassPack())
										.Merge(new PluralizedTablesPack(_orm, new EnglishInflector()))
										.Merge(new ListIndexAsPropertyPosColumnNameApplier())
										.Merge(new DatePropertyByNameApplier())
										.Merge(new MsSQL2008DateTimeApplier())
										.Merge(new NoPoidGuidApplier());

			_orm.Patterns.PoidStrategies.Add(new NativePoidPattern());
			_mapper = new Mapper(_orm, patternsAppliers);
		}

		#region Implementation of IDomainMapper

		public ObjectRelationalMapper DomainDefinition
		{
			get { return _orm; }
		}

		public Mapper Mapper
		{
			get { return _mapper; }
		}

		#endregion
	}
}
